/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.gui;

import horizon.component.IRenderUpdate;
import horizon.graphics.RenderingContext;
import horizon.gui.component.GuiComponent;
import horizon.gui.component.GuiTextField;
import horizon.gui.component.GuiWindow;
import horizon.states.State;
import horizon.util.InputManager;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public abstract class GuiOverlay implements IRenderUpdate {

    State owner;
    private ArrayList<GuiComponent> children = new ArrayList<>();
    GuiComponent focused = null;
    boolean backLastUpdate = false;

    public GuiOverlay(State owner) {

        this.owner = owner;

    }

    public void addComponent(GuiComponent g) {

        children.add(g);

    }

    public void update(long elapsedTime) {
        MouseEvent e = InputManager.getInstance().getLastClick();

        Point p = null;
        if (e != null) {
            System.out.println("Mouse clicked at " + e.getPoint());
            p = new Point(e.getX(), e.getY());
        }

        if (focused != null) {
            if (focused instanceof GuiTextField) {

                ((GuiTextField) focused).setText(InputManager.getInstance().getKeyCache());
                InputManager.getInstance().setBlocked(false);
                boolean b = true;
                if (InputManager.getInstance().isKeyDown(KeyEvent.VK_ENTER)) {

                    if (focused != null) {

                        focused.enterPressed();
                        focused.setInFocus(false);
                        focused = null;
                        b = false;

                    }

                }
                InputManager.getInstance().setBlocked(b);
            } else if (focused instanceof GuiWindow) {
                
                //InputManager.getInstance().getLastDrag();
                
            }
        }




        if (!children.isEmpty()) {

            boolean compClicked = false;

            for (GuiComponent g : children) {//Begin comp loop

                g.update(elapsedTime);

                if (p != null && g.isClicked(p)) {

                    System.out.println(g.getName() + " is in focus");
                    g.setInFocus(true);
                    focused = g;
                    InputManager.getInstance().clearKeyCache();
                    g.onActivate(e);
                    owner.actionGui(g.getName());
                    compClicked = true;

                }

            }//End comp loop

            if (compClicked == true) {

                InputManager.getInstance().setBlocked(true);
                for (GuiComponent g : children) {

                    if(!focused.equals(g)){
                        g.setInFocus(false);
                    }
   
                }

            } else if(compClicked == false && p == null){
                
                
                
            }
            else {
                for (GuiComponent g : children) {

                    g.setInFocus(false);

                }
                focused = null;
                InputManager.getInstance().setBlocked(false);

            }

        }

    }

    @Override
    public void render(RenderingContext r) {

        if (!children.isEmpty()) {

            for (GuiComponent g : children) {

                g.render(r);

            }

        }

    }

    public void removeComponent(String name) {

        for (GuiComponent g : children) {

            if (g.getName().equals(name)) {

                children.remove(g);
                return;

            }

        }

    }
}
